<style>
    .box {
        padding: 15px;
    }

    .searchbtn {
        margin-left: 20px;
        display: inline-block;
        border: 1px solid #ddd;
        font-size: 0.8em;
        line-height: 25px;
        background: #d9434e;
        padding-left: 15px;
        padding-right: 15px;
        color: #fff;
        border-radius: 5px;
    }

    input[type=date] {
        margin-right: 10px;
    }

    table {
        box-sizing: border-box;
        margin-top: 20px;
    }

    th, td {
        line-height: 40px;
        text-align: center;
    }

    thead, tr {
        border-bottom: 1px dashed #ddd;
    }
</style>
<?php
    Distribute::autoConfirm($minfo['member_id']);


?>
<link rel="stylesheet" href="/packages/resource/pintuer/pintuer.css">
<div class="box">
    <div class="pintuer">
        <form action="" class="search form-x">
            <input type="hidden" name="action" value="show_order">

            <div class="form-group">
                <div class="label x4">
                    开始时间：
                </div>
                <div class="field x8">
                    <input type="date" name="start_time" class="input">

                </div>
            </div>
            <div class="form-group">
                <div class="label x4">
                    结束时间：
                </div>
                <div class="field x8">
                    <input type="date" name="end_time" class="input">

                </div>
            </div>

            <div class="form-group">
                <div class="label x4">
                    订单状态：
                </div>
                <div class="field x8">
                    <select class="input" name="order_state" id="">
                        <option value="">请选择</option>
                        <option value="10">未付款</option>
                        <option value="20,30">已付款</option>
                        <option value="40">已收货</option>
                        <option value="50">退换</option>
                    </select>
                </div>
            </div>

            <div class="text-center">
                <button class="button bg-blue" style="width: 100%;" type="submit">搜索</button>
            </div>


        </form>
    </div>

    <div class="pintuer">
        <div class="text-left margin-top">
            <p>
                总佣金：
                <span class="text-red">
                     ￥<?php
                         $res = ml()->query("select sum(margin) as total from ra_weproxy_order_margin where member_id = " . $minfo['member_id'] . '');
                        if (empty($res[0]['total'])) {
                            echo 0;
                        }
                        else{
                            echo $res[0]['total'];
                        }
                     ?>
                </span>

            </p>
            <p>
                订单未付款佣金：
                <span class="text-red">
                    ￥
                    <?php

                        $res = ml()->query("select sum(wm.margin) as total from ra_weproxy_order_margin wm left join ra_order o on o.order_id = wm.order_id where o.order_state = 10 and wm.member_id = " . $minfo['member_id'] . '');
                        if (empty($res[0]['total'])) {
                            echo 0;
                        }
                        else{
                            echo $res[0]['total'];
                        }
                    ?>
                </span>
            </p>
            <p>
                已付款未到账的佣金：
                <span class="text-red">
                    ￥
                    <?php
                        $res = ml()->query("select sum(wm.margin) as total from ra_weproxy_order_margin wm left join ra_order o on o.order_id = wm.order_id where (o.order_state = 20 or o.order_state = 30) and wm.member_id = " . $minfo['member_id'] . '');
                        if (empty($res[0]['total'])) {
                            echo 0;
                        }
                        else{
                            echo $res[0]['total'];
                        }
                    ?>
                </span>
            </p>
        </div>

        <style>
            .goods_list img {
                width: 80px;
                height: 80px;
                float: left;
            }

            .goods_list span {
                float: left;
            }
        </style>
        <table class="table table-bordered">
            <tr class="thead">
                <!--            <th>订单编号</th>-->
                <th>商品</th>
                <th>佣金</th>
            </tr>
            <?php
                $m = Model()->table('order,weproxy_order_margin')->join('left')->on('order.order_id = weproxy_order_margin.order_id');
                $condition = array();
                $minfo = get_mobile_member_info();
                $condition['weproxy_order_margin.member_id'] = $minfo['member_id'];
                if (!empty($_GET['start_time']) && !empty($_GET['end_time'])) {
                    $condition['weproxy_order_margin.time'] = array(
                        'between',
                        array(
                            strtotime($_GET['start_time']),
                            strtotime($_GET['end_time'])
                        )
                    );

                } else if (!empty($_GET['start_time'])) {
                    $condition['weproxy_order_margin.time'] = array(
                        'gt',
                        strtotime($_GET['start_time'])
                    );
                } else if (!empty($_GET['end_time'])) {
                    $condition['weproxy_order_margin.time'] = array(
                        'lt',
                        strtotime($_GET['end_time'])
                    );
                }

                if ($_GET['order_state'] > 0) {
                    if ($_GET['order_state'] == 50) {
                        $condition['order.refund_state'] = [
                            'gt',
                            0,
                        ];
                    } else if (strpos($_GET['order_state'], ',') !== false) {
                        $condition['order.order_state'] = [
                            'in',
                            explode(',', $_GET['order_state'])
                        ];
                    } else {
                        $condition['order.order_state'] = $_GET['order_state'];
                    }

                }


                $list = $m->where($condition)->page(20)->select();

                if ($list) {
                    foreach ($list as $item) {
                        ?>
                        <tr>
                            <td colspan="100" class="text-left" style="background: #eee;">
                                订单编号：
                                <?php echo $item['order_sn'] ?>
                                <br>
                                下单时间：
                                <?php echo date('Y-m-d H:i:s', $item['add_time']); ?>
                                <br>
                                购买人：
                                <?php
                                    $_minfo = ml('member')->find($item['from_member']);
                                    echo $_minfo['member_name'];
                                ?>
                                <br>
                                订单状态：
                                <span class="text-blue">

                                <?php
                                    if ($item['refund_state'] > 0) {
                                        echo "退换";
                                    }
                                    else if ($item['order_state'] == 10) {
                                        echo "未付款";
                                    }
                                    else if ($item['order_state'] == 20) {
                                        echo "已付款";
                                    }
                                    else if ($item['order_state'] == 30) {
                                        echo "已付款";
                                    }
                                    else if ($item['order_state'] == 40) {
                                        echo "已完成";
                                    }


                                ?>
                                </span>

                            </td>
                        </tr>
                        <tr>
                            <td class="goods_list">
                                <?php
                                    $og = Model('order')->getOrderGoodsList(['order_id' => $item['order_id']]);
                                    foreach ($og as $g) {
                                        ?>
                                        <div>
                                            <div class="float-left">
                                                <img src="<?php echo thumb($g) ?>" alt="">
                                            </div>
                                            <div class="float-left">
                                                <p class="text-left">
                                                    <?php echo $g['goods_name'] ?>
                                                </p>

                                                <p class="text-left ">
                                                    <span class="text-red">
                                                    ￥ <?php echo $g['goods_price'] ?>
                                                    </span>
                                                    <span class="margin-left">
                                                     X <?php echo $g['goods_num'] ?>
                                                    </span>
                                                </p>
                                            </div>


                                        </div>
                                        <?php
                                    }
                                ?>
                            </td>



                            <td>
                                ￥
                                <?php
                                    echo $item['margin'];
                                ?>
                            </td>
                        </tr>
                        <?php
                    }
                }

            ?>
        </table>
        <link rel="stylesheet" href="/packages/resource/css/pager.css">
        <div class="_pager margin-big-top margin-big-bottom">
            <?php
                echo $m->showpage();
            ?>
        </div>
        <div style="height: 50px;">

        </div>
    </div>

</div>